package edu.tongji.jiaowu.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.tongji.quickCRUDQ.define.Row;
import com.tongji.quickCRUDQ.exception.FormatException;
import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.service.QuickStructureManager;
import com.tongji.quickCRUDQ.service.impl.BaseManagerImpl;

import edu.tongji.jiaowu.dao.OriginalScoreCourseDao;
import edu.tongji.jiaowu.dao.OriginalScoreDao;
import edu.tongji.jiaowu.dao.ScoreDao;
import edu.tongji.jiaowu.service.OriginalScoreCourseManager;
import edu.tongji.jiaowu.service.ScoreLevelManager;

public class OriginalScoreCourseManagerImpl extends BaseManagerImpl implements OriginalScoreCourseManager {
	private QuickStructureManager quickStructureManager;
	private ScoreLevelManager scoreLevelManager;
	private OriginalScoreCourseDao originalScoreCourseDao;
	private OriginalScoreDao originalScoreDao;
	private ScoreDao scoreDao;

	public CommonBean createOriginalScoreCourse() {
		return originalScoreCourseDao.createOriginalScoreCourse();
	}

	/*
	 public CommonBean findOriginalScoreCourses(String refid) {
	 return originalScoreCourseDao.findOriginalScoreCourses(refid);
	 }
	 */
	public CommonBean findOriginalScoreCourseById(String id) {
		return originalScoreCourseDao.findOriginalScoreCourseById(id);
	}

	public CommonBean findAllOriginalScoreCourses() {
		return originalScoreCourseDao.findAllOriginalScoreCourses();
	}

	public void updateAndConfirm(List updtData, Row row) {
    	if(updtData==null||updtData.size()==0) return;
    	getCommonDao().templateBatchUpdate(row, updtData);
    	CommonBean originalScoreAll = originalScoreDao.createOriginalScore();
    	originalScoreAll.addData(updtData);
    	int s1=originalScoreAll.getSize();
    	for (int i = 0; i < s1; i++) {
			String id=originalScoreAll.getId(i);
			String refid=originalScoreAll.getPropertyAsString(i, "refid");
			CommonBean originalScoreCourse = originalScoreCourseDao.findOriginalScoreCourseById(refid);
			CommonBean originalScore=originalScoreAll.query(id);
			submitScore(originalScoreCourse, originalScore);
		}
	}
	
	public void confirm(String id) {
		CommonBean originalScoreCourse = originalScoreCourseDao.findOriginalScoreCourseById(id);
		if(originalScoreCourse.getSize()!=1){
			throw new FormatException("没有id为'"+id+"'的待确认成绩的课程");
		}
		String status=originalScoreCourse.getPropertyAsString(0, "status");
		if("Y".equals(status)){
			throw new FormatException("该课程成绩已经确认!");
		}
//		originalScoreCourse.setProperty(0, "status", "Y");
//		originalScoreCourse.setProperty(0, "entryDate", new Date());
//		quickStructureManager.update(originalScoreCourse.getRow(), originalScoreCourse.getData());
		
		CommonBean originalScore = originalScoreDao.findOriginalScore(id);
		submitScore(originalScoreCourse, originalScore);//提交成绩
		originalScoreCourseDao.updateOriginalScoreCourse("Y",id);//修改成绩课程状态
	}

	private void submitScore(CommonBean originalScoreCourse, CommonBean originalScore) {
		String schoolYear=originalScoreCourse.getPropertyAsString(0, "schoolYear");//学年度
		String schoolSemester=originalScoreCourse.getPropertyAsString(0, "schoolSemester");//学期
		String courseNo=originalScoreCourse.getPropertyAsString(0, "courseNo");//课号
		String courseName=originalScoreCourse.getPropertyAsString(0, "courseName");//课程名称
		String classNo=originalScoreCourse.getPropertyAsString(0, "classNo");//课程序号
		String courseType=originalScoreCourse.getPropertyAsString(0, "courseType");//课程类型
		String courseNature=originalScoreCourse.getPropertyAsString(0, "courseNature");//课程性质
		String examType=originalScoreCourse.getPropertyAsString(0, "examType");//考核方式
		String scoreFormat=originalScoreCourse.getPropertyAsString(0, "scoreFormat");//成绩形式
		String credit=originalScoreCourse.getPropertyAsString(0, "credit");//学分
		CommonBean insertScore = scoreDao.createScore();//插入成绩
		CommonBean updateScore = scoreDao.createScore();//修改成绩
		CommonBean refreshScore = scoreDao.createScore();//刷新成绩修改时间
		List deleteScore=new ArrayList();
		int s1 = originalScore.getSize();
		for (int i = 0; i < s1; i++) {
			String replacedCourseNo=originalScore.getPropertyAsString(i, "replacedCourseNo");//被代替课号
			String scoreCourseNo=replacedCourseNo==null?courseNo:replacedCourseNo;//学生成绩课程代码
			String studentNo = originalScore.getPropertyAsString(i, "studentNo");
			String score = originalScore.getPropertyAsString(i, "score");
			String scoreNature = originalScore.getPropertyAsString(i, "scoreNature");
			if ("L".equals(scoreNature)) {//改选
			    String[] delete=new String[]{studentNo,courseNo};	
			    deleteScore.add(delete);
				continue;
			}
			
			String scoreType = originalScore.getPropertyAsString(i, "scoreType");
			CommonBean scoreLevel=scoreLevelManager.findScoreLevel(score, scoreFormat);
			BigDecimal gradePoint=(BigDecimal) scoreLevel.getProperty(0, "gradePoint");
			String isPass=scoreLevel.getPropertyAsString(0, "isPass");
			
			CommonBean historyScore=scoreDao.findScore(scoreCourseNo,studentNo);
			int s2=historyScore.getSize();
			if (s2==0) {
				insertScore.addBean(new HashMap());
				insertScore.push("scoreNature", scoreNature);//成绩性质
				insertScore.push("studentNo", studentNo);//学号
				insertScore.push("score", score);//成绩
				insertScore.push("credit", credit);//学分
				insertScore.push("gradePoint", gradePoint);//绩点
				insertScore.push("isPass", isPass);//是否通过
				insertScore.push("schoolYear", schoolYear);//学年度
				insertScore.push("schoolSemester", schoolSemester);//学期
				insertScore.push("courseNo", scoreCourseNo);//课程代码
				insertScore.push("classNo", classNo);//课程序号
				insertScore.push("courseName", courseName);//课程名称
				insertScore.push("courseType", courseType);//课程类型
				insertScore.push("courseNature", courseNature);//课程性质
				insertScore.push("examType", examType);//考核方式
				insertScore.push("scoreFormat", scoreFormat);//成绩形式
				insertScore.push("updateTime", new Date());//更新时间
			} else {
				BigDecimal historyGradePoint=(BigDecimal) historyScore.getProperty(0, "gradePoint");//原绩点
				String historyScoreLevel=historyScore.getPropertyAsString(0, "score");//原成绩
				String historyScoreNature=historyScore.getPropertyAsString(0, "scoreNature");//原成绩性质
				String historyScoreFormat=historyScore.getPropertyAsString(0, "scoreFormat");//原成绩形式
				String historyIsPass=historyScore.getPropertyAsString(0, "isPass");//原成绩是否通过
				boolean updated=false;
				int cm=gradePoint.compareTo(historyGradePoint);
				if ("02".equals(scoreFormat)&&"02".equals(historyScoreFormat)) {//历史成绩与新成绩都是百分制，则比较百分制的成绩
					BigDecimal historyScoreDecimal=new BigDecimal(historyScore.getPropertyAsString(0, "score"));
					BigDecimal scoreDecimal=new BigDecimal(score);
					cm=scoreDecimal.compareTo(historyScoreDecimal);
				}
				if(cm==1){//新绩点大于原绩点(或百分制下新的分数大于原分数)
					updated=true;
				}else if(cm==0&&gradePoint.compareTo(new BigDecimal("0"))==0&&!"G".equals(scoreNature)){//新绩点等于原绩点并且都等于0(或百分制下新的分数等于原分数),成绩性质不为缓考
					if ("H".equals(scoreNature)) {//新成绩性质为"作弊"
						updated=true;
					} else if("-5".equals(score)&&"0".equals(historyScoreLevel)) {//新成绩为"不及格",原成绩为"无成绩"
						updated=true;
					} else if("-5".equals(score)&&"02".equals(historyScoreFormat)&&"N".equals(historyIsPass)) {//新成绩为"不及格",原成绩为"百分制"且"未通过"
						updated=true;
					}else if("I".equals(scoreNature)&&"G".equals(historyScoreNature)){//新成绩性质为"旷考",原成绩性质为"缓考"
						updated=true;
					}
				}
				if (updated) {
					updateScore.addBean(new HashMap());
					updateScore.push("scoreNature", scoreNature);//成绩性质
					updateScore.push("score", score);//成绩
					updateScore.push("gradePoint", gradePoint);//绩点
					updateScore.push("isPass", isPass);//是否通过
					updateScore.push("scoreFormat", scoreFormat);//成绩形式
					updateScore.push("updateTime", new Date());//更新时间
					updateScore.pushId(historyScore.getId(0));//ID
				}else{
					refreshScore.addBean(new HashMap());
					refreshScore.push("updateTime", new Date());//更新时间
					refreshScore.pushId(historyScore.getId(0));//ID
				}
			}
		}
		if(insertScore.getSize()>0){
			scoreDao.insertScore(insertScore);
		}
		if(updateScore.getSize()>0){
			scoreDao.updateScore(updateScore);
		}
		if(refreshScore.getSize()>0){
			scoreDao.updateScore(refreshScore);
		}
		if (deleteScore.size()>0) {
			scoreDao.deleteScore(deleteScore);
		}
	}

	public void insertOriginalScoreCourse(CommonBean originalScoreCourseBean) {
		originalScoreCourseDao.insertOriginalScoreCourse(originalScoreCourseBean);
	}

	public OriginalScoreCourseDao getOriginalScoreCourseDao() {
		return originalScoreCourseDao;
	}

	public void setOriginalScoreCourseDao(OriginalScoreCourseDao originalScoreCourseDao) {
		this.originalScoreCourseDao = originalScoreCourseDao;
	}

	public QuickStructureManager getQuickStructureManager() {
		return quickStructureManager;
	}

	public void setQuickStructureManager(QuickStructureManager quickStructureManager) {
		this.quickStructureManager = quickStructureManager;
	}

	public OriginalScoreDao getOriginalScoreDao() {
		return originalScoreDao;
	}

	public void setOriginalScoreDao(OriginalScoreDao originalScoreDao) {
		this.originalScoreDao = originalScoreDao;
	}

	public ScoreDao getScoreDao() {
		return scoreDao;
	}

	public void setScoreDao(ScoreDao scoreDao) {
		this.scoreDao = scoreDao;
	}

	public ScoreLevelManager getScoreLevelManager() {
		return scoreLevelManager;
	}

	public void setScoreLevelManager(ScoreLevelManager scoreLevelManager) {
		this.scoreLevelManager = scoreLevelManager;
	}



}
